﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace UI.Dendrogram
{
    public class HeatMapCell:Canvas
    {
        #region events
        public event OnHeatMapCellSelected HeatMapCellSelected;
        #endregion

        #region UI
        public Color BackColor { get; set; }
        private readonly TextBlock _TextBlock;
        public Rectangle BoundingBox { get; set; }
        private readonly TextBlock _TooltipBlock;
        #endregion

        #region Data
        public string Label { get; set; }
        public string ToolTipText { get; set; }
        public Branch RowBranch { get; set; }
        public Branch ColumnBranch{ get; set; }
        #endregion

        #region ctor
        public HeatMapCell(
            Canvas parent, BranchArm rowArm, BranchArm colArm,
            Color color, string label, string tooltip)
        {
            this.BackColor = color;
            this.Label = label;
            this.ToolTipText = tooltip;
            double x = (double)colArm.Branch.BoundingBox.GetValue(Canvas.LeftProperty);
            if (!colArm.IsLeft)
                x += colArm.Branch.BoundingBox.Width / 2;
            double y = (double) rowArm.Branch.BoundingBox.GetValue(Canvas.TopProperty);
            if (!rowArm.IsLeft)
                y += rowArm.Branch.BoundingBox.Height / 2;
            double w = colArm.Branch.BoundingBox.Width / 2;
            double h = rowArm.Branch.BoundingBox.Height / 2;
            this.Clip = new RectangleGeometry() {Rect = new Rect(x, y, w, h)};
            this.RowBranch = rowArm.Branch;
            this.ColumnBranch = colArm.Branch;

            this.BoundingBox = new Rectangle();
            parent.Children.Add(this.BoundingBox);
            this.BoundingBox.SetValue(Canvas.LeftProperty, x);
            this.BoundingBox.SetValue(Canvas.TopProperty, y);
            this.BoundingBox.SetValue(WidthProperty, w);
            this.BoundingBox.SetValue(HeightProperty, h);
            this.BoundingBox.Fill = new SolidColorBrush(this.BackColor);

            this._TextBlock=new TextBlock();
            this._TextBlock.Text = label;
            parent.Children.Add(this._TextBlock);
            this._TextBlock.SetValue(Canvas.LeftProperty,x);
            this._TextBlock.SetValue(Canvas.TopProperty, y);

            this._TooltipBlock=new TextBlock();
            this._TooltipBlock.Text = this.ToolTipText;
            ToolTipService.SetToolTip(this.BoundingBox,this._TooltipBlock);

            this.BoundingBox.MouseEnter += new MouseEventHandler(OnMouseEnter);
            this.BoundingBox.MouseLeave+=new MouseEventHandler(OnMouseLeave);
            this.BoundingBox.MouseLeftButtonDown += new MouseButtonEventHandler(OnClick);
        }

        void OnClick(object sender, MouseButtonEventArgs e)
        {
            if (this.HeatMapCellSelected != null)
                this.HeatMapCellSelected(this.RowBranch, this.ColumnBranch);
        }

        void OnMouseEnter(object sender, MouseEventArgs e)
        {
            this.BoundingBox.Fill = new SolidColorBrush(Colors.Red);
        }

        void OnMouseLeave(object sender, MouseEventArgs e)
        {
            this.BoundingBox.Fill = new SolidColorBrush(this.BackColor);
        }
        #endregion
    }
}
